<template>
  <div>
    <div>
      <el-card>
        <el-row class="but">
          <el-button type="primary" icon="el-icon-plus" @click="dialogVisible = true">新增角色</el-button>
        </el-row>
        <el-table
          :data="list"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            :index="indexMethod"
            prop="companyId"
            label="序号"
            width="80"
          />
          <el-table-column
            prop="name"
            label="角色名称"
            width="100"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column align="center" label="操作" width="500">

            <template v-slot="slot">
              <el-button @click="clickShowAssignDialog(slot.row.id)">分配权限</el-button>
              <el-button @click.native="etidsetting(slot.row.id)">编辑</el-button>
              <el-button type="danger" @click.native="delsetting(slot.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 编辑弹层 -->
    <el-dialog
      title="编辑弹层"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form ref="form" label-width="120px" :model="Form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="Form.name" style="width:80%" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="Form.description" style="width:80%" placeholder="请描述一下" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click.native="confirm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-pagination
      stype="flex"
      justify="center"
      align="middle"
      :page-sizes="[5, 10, 15, 20, 25]"
      :page-size="params.pagesize"
      :current-page="params.page"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @open="hGetPermissionList"
      @close="closeAssignDialog"
    >
      <el-tree
        ref="reftree"
        :data="permissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <!-- 权限点数据展示 -->
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showAssignDialog = false">取消</el-button>
          <el-button type="primary" @click="clicksetting">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { settingAPI, addsettingAPI,
  delsettingAPI, getsettingAPI,
  updatesettingAPI, permissionsettingAPI, permissionstingAPI
} from '@/api/Setting'
import tree from '@/utils/transTree.js'
export default {
  data() {
    return {
      list: [],
      permissionList: [], // 存储权限数据
      // setlist: [],
      dialogVisible: false,
      showAssignDialog: false, // 控制弹层的显示隐藏
      params: {
        page: 1,
        pagesize: 5
      },
      // tableData: [{
      //   num: '1',
      //   name: '王小虎',
      //   address: '上海市普陀区金沙江路 1518 弄'
      // }, {
      //   num: '2',
      //   name: '王小虎',
      //   address: '上海市普陀区金沙江路 1517 弄'
      // }, {
      //   num: '3',
      //   name: '王小虎',
      //   address: '上海市普陀区金沙江路 1519 弄'
      // }, {
      //   num: '4',
      //   name: '王小虎',
      //   address: '上海市普陀区金沙江路 1516 弄'
      // }],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] }
        ]
      },
      Form: {
        name: '',
        description: '',
        id: ''
      },
      total: 0,
      cuID: ''
    }
  },
  mounted() {
    this.settinglist()
    // this.getsetting()
  },
  methods: {
    async clickShowAssignDialog(id) {
      this.cuID = id
      this.showAssignDialog = true
    },
    // indexMethod(index) {
    //   return index * 2
    // },
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pagesize + index + 1
    },

    handleSizeChange(val) {
      this.params.pagesize = val
      console.log(val)
      this.settinglist()
    },
    handleCurrentChange(val) {
      this.params.page = val
      console.log(val)
      this.settinglist()
    },
    async settinglist() {
      const res = await settingAPI(this.params)
      this.list = res.rows
      this.total = res.total
    },
    closeDialog() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },

    // 兜底校验
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.Form.id) {
            await updatesettingAPI(this.Form)
          } else {
            await addsettingAPI(this.Form)
          }
          this.$message.success(`${this.Form.id ? '编辑' : '新增'}成功`)
          this.settinglist()
          this.dialogVisible = false
          this.$refs.form.resetFields()
          this.resform()
        }
      })
    },
    resform() {
      this.Form = {
        name: '',
        description: '',
        id: ''
      }
    },

    async delsetting(id) {
      await delsettingAPI(id)
      console.log(id)
      this.settinglist()
    },
    // getsetting
    // async getsetting(id) {
    //   this.dialogVisible = true
    //   const res = await getsettingAPI(this.Form)
    //   this.settinglist()
    //   this.Form = res
    // }
    async etidsetting(id) {
      this.dialogVisible = true
      // console.log(id)
      const res = await getsettingAPI(id)
      this.Form = res
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },

    async hGetPermissionList() {
      // 发送请求, 获取权限列表
      const res = await permissionsettingAPI()
      this.permissionList = tree(res)
      const detail = await getsettingAPI(this.cuID)
      this.$refs.reftree.setCheckedKeys(detail.permIds)
    },
    async clicksetting() {
      const ids = this.$refs.reftree.getCheckedKeys()
      console.log(ids)
      await permissionstingAPI({
        id: this.cuID,
        permIds: this.ids
      })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    }

  }
}
</script>
<style>

</style>
